<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$setting = $this->getSetting();

if ($operation == 'display') {
    $list = pdo_fetchall("SELECT * FROM " . tablename("weisrc_dish_shouqianba_code") . " WHERE weid = :weid  ORDER BY id DESC", array(':weid' => $_W['uniacid']));

} elseif ($operation == 'post') {
    load()->func('tpl');
    $id = intval($_GPC['id']);
    if (!empty($id)) {
        $school = pdo_fetch("SELECT * FROM " . tablename("weisrc_dish_shouqianba_code") . " WHERE id = :id", array(':id' => $id));
    } else {
        $school = array(
            'displayorder' => 0,
        );
    }

    if (checksubmit('submit')) {
        if (empty($_GPC['code'])) {
            message('抱歉，请输入激活码！');
        }

        $data = array(
            'weid' => $_W['uniacid'],
            'code' => $_GPC['code'],
            'device_id' => $_GPC['device_id'],
            'dateline' => TIMESTAMP
        );

        if (!empty($id)) {
            pdo_update("weisrc_dish_shouqianba_code", $data, array('id' => $id));
        } else {
            pdo_insert("weisrc_dish_shouqianba_code", $data);
            $id = pdo_insertid();
        }
        message('更新成功！', $this->createWebUrl('shouqianbacode', array('op' => 'display')), 'success');
    }
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $area = pdo_fetch("SELECT id FROM " . tablename("weisrc_dish_shouqianba_code") . " WHERE id = {$id}");
    if (empty($area)) {
        if ($area['result_code'] == '200') {

        }
        message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('school', array('op' => 'display')), 'error');
    }
    pdo_delete("weisrc_dish_shouqianba_code", array('id' => $id));
    message('删除成功！', $this->createWebUrl('shouqianbacode', array('op' => 'display')), 'success');
} elseif ($operation == 'setstatus') {
    $id = intval($_GPC['id']);
    $item = pdo_fetch("SELECT * FROM " . tablename("weisrc_dish_shouqianba_code") . " WHERE id = {$id}");
    if ($item) {
        include '../addons/weisrc_dish_plugin_shouqianba/shouqianba.php';
        $shouqianba = new shouqianba($setting['shouqianba_vendor_sn'], $setting['shouqianba_vendor_key'], $setting['shouqianba_vendor_appid']);
        $result = $shouqianba->activate($item['code'], $item['device_id']);
        $result = json_decode($result, true);
        if ($result['result_code'] == '200') {
            pdo_update("weisrc_dish_shouqianba_code",
                array(
                    'terminal_sn' => $result['biz_response']['terminal_sn'],
                    'terminal_key' => $result['biz_response']['terminal_key'],
                    'updatetime' => TIMESTAMP,
                    'result_code' => '200'
                    ),
                array('id' => $id)
            );
            message('激活成功！', $this->createWebUrl('shouqianbacode', array('op' => 'display')), 'success');
        } else {
            pdo_update("weisrc_dish_shouqianba_code",
                array(
                    'result_code' => $result['result_code']
                ),
                array('id' => $id)
            );
            message('激活失败！!', $this->createWebUrl('shouqianbacode', array('op' => 'display')), 'error');
        }
    }
}
include $this->template('web/shouqianbacode');